//
//  DHLeftViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2017/12/27.
//  Copyright © 2017年 张朋. All rights reserved.
//

#import "DHLeftViewController.h"
#import "DHSettingViewController.h"
#import "DHLeftControllerTableViewCell.h"
#import "DHUserManagerViewController.h"
#import "DHFashionWearViewController.h"

@interface DHLeftViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIButton *vipButton;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *dataArr;
@property (strong, nonatomic) NSArray *iconImgsArr;

@end

@implementation DHLeftViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self initView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArr = @[@[@"订单管理"],@[@"时尚穿搭",@"客服",@"通知",@"收藏",@"常见问题"]];
    self.iconImgsArr = @[@[@"menu_shoppingcart"],@[@"menu_fashion",@"menu_cs",@"menu_notice",@"menu_collect",@"menu_problem"]];
    
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHLeftControllerTableViewCell" bundle:nil] forCellReuseIdentifier:kDHLeftControllerTableViewCell];
}

- (void)initView{
    [self.headImageView dh_setImageURLString:[Login curLoginUser].photo];
    if ([Login curLoginUser].nickname.length > 0) {
        self.nameLabel.text = [Login curLoginUser].nickname;
    }else{
        self.nameLabel.text = [Login curLoginUser].phone;
    }
    if ([GlobalManager shareGlobalManager].vip) {
        NSLog(@"是vip");
    }else{
        NSLog(@"不是vip");
        self.vipButton.hidden = YES;
    }
}

// 设置
- (IBAction)settingAction:(id)sender {
    DHSettingViewController *settingController = [[DHSettingViewController alloc] init];
//    DHNavigationViewController *navigationController = [[DHNavigationViewController alloc] initWithRootViewController:settingController];
//    [self presentViewController:navigationController animated:YES completion:nil];
    [self.navigationController pushViewController:settingController animated:YES];
}

// 登出
- (IBAction)logoutAction:(id)sender {
    [UIAlertView bk_showAlertViewWithTitle:@"登出账户" message:@"您确定要登出账户？Dresshere会不定期提供特惠惊喜哦！" cancelButtonTitle:@"取消" otherButtonTitles:@[@"登出"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex == 0) {
            [alertView setHidden:YES];
        }else{
            [[DHNetAPIClient shareDHNetAPIClient] request_logoutAndBlock:^(id data, NSError *error) {
//                [[DHNetAPIClient shareDHNetAPIClient].requestSerializer setValue:@"" forHTTPHeaderField:@"token"];
                [Login doLogout];
                [kAppDelegate configHomeController];
            }];
        }
    }];
}

// 用户管理
- (IBAction)checkMemberAction:(id)sender {
    DHUserManagerViewController *umController = [[DHUserManagerViewController alloc] init];
    DHNavigationViewController *navigationController = [[DHNavigationViewController alloc] initWithRootViewController:umController];
    [self presentViewController:navigationController animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArr[section] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.dataArr count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHLeftControllerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHLeftControllerTableViewCell forIndexPath:indexPath];
    cell.titleLabel.text = self.dataArr[indexPath.section][indexPath.row];
    [cell.iconImageView setImage:[UIImage imageNamed:self.iconImgsArr[indexPath.section][indexPath.row]]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0 && indexPath.row == 0) {
        
    }else if (indexPath.section == 1 && indexPath.row == 0){
        DHFashionWearViewController *fashionController = [[DHFashionWearViewController alloc] init];
        DHNavigationViewController *navigationController = [[DHNavigationViewController alloc] initWithRootViewController:fashionController];
        [self presentViewController:navigationController animated:YES completion:nil];
    }
}

//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;//section头部高度
}
//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
    view.backgroundColor = [UIColor clearColor];
    return view ;
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 40;
}
//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    view.backgroundColor = [UIColor clearColor];
    if (section == 0) {
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(18, 19.5, self.view.width, 1)];
        lineView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        [view addSubview:lineView];
    }
    return view;
}

@end
